<template>
	<el-main class="elmain" style="background-color: #FFFFFF;border-radius: 4px; ">
		<el-row>
			<el-col :span="8"></el-col>
			<el-col :span="8"></el-col>
			<el-col :span="8">
				<el-button size="small" type="warning" v-if="ifselect" @click="selectSave">选中保存</el-button>
				<el-button size="small" type="warning" @click="allSave">全部保存</el-button>
			</el-col>
		</el-row>
		<el-table :data="news" height="600" style="width: 100%" @selection-change="handleSelectionChange" >
			<el-table-column type="selection" prop="sessionId" width="55" :selectable="selectable" />
			<el-table-column prop="newsId" label="消息编号"></el-table-column>
		    <el-table-column prop="newsSendTime" label="发送时间"></el-table-column>
			<el-table-column prop="sendUserId" label="发送方ID"></el-table-column>
			<el-table-column prop="acceptUserId" label="接受方ID"></el-table-column>
			<el-table-column prop="newsText" label="消息内容" :show-overflow-tooltip="true"/>
	 </el-table>
	</el-main>
</template>

<script>
	import qs from 'qs'
export default{
	data(){
		return{
			ifselect:false,
			user:{},
			session:{},
			selectrow:[],
			news:[],
			saved:[]
		}
	},
	methods:{
		selectable(row){
			if(this.saved){
				if(this.saved.indexOf(row.newsId)>=0){
					return false
				}
				else{
					return true
				}
			}
			else{return true}
		},
		handleSelectionChange(arr,row){
			if(arr.length!=0){this.ifselect=true}
			else{this.ifselect=false}
			this.selectrow=arr
			console.log(this.selectrow)
		},
		selectSave(){
				let that=this
				var save=[]
				for(var s in that.selectrow){
					var  update={}
					update.newsId=that.selectrow[s].newsId
					update.sendUserId=that.user.userId
					save.push(update.newsId)
					let str=qs.stringify(update)
					that.$axios.get("http://localhost:8080/news/savenews?"+str).then(function(res){console.log(res)})
					
				}
				
				sessionStorage.setItem("savednews",JSON.stringify(save))
				this.$router.go(0)
				//that.$axios.get("http://localhost:8080/session/sessionshow?userId="+that.user.userId)
				//console.log(e)
			},
			allSave(){
				let that=this
				var save=[]
				for(var s in that.news){
					var  update={}
					update.newsId=that.news[s].newsId
					update.sendUserId=that.user.userId
					save.push(update.newsId)
					let str=qs.stringify(update)
					that.$axios.get("http://localhost:8080/session/sessionupdate?"+str).then(function(res){console.log(res)})
					
				}
				sessionStorage.setItem("savednews",JSON.stringify(save))
				this.$router.go(0)
			}
			
		},
		
	
	created(){
		const s=JSON.parse(sessionStorage.getItem("sessionnews"))
		console.log(s.sessionId)
		this.session.sessionId=s.sessionId
		this.session.userId=s.bothid
		this.session.userAvatar=s.bothA
		this.session.seeionId=s.sessionId
		let loginUser=JSON.parse(sessionStorage.getItem("user"))
		this.user=loginUser
		
		let that=this
		const rule={}
		rule.sessionId=that.session.sessionId
		var str=qs.stringify(rule)
		that.$axios.get("http://localhost:8080/session/sessionnews?"+str).then(function(res){
			that.news=res.data
		})
		that.saved=JSON.parse(sessionStorage.getItem("savednews"))
		console.log(that.saved)
	}
}
</script>

<style>
</style>